package com.lagou.rpc;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;

import java.util.List;

public class RpcEncoder2 extends MessageToMessageEncoder<RpcRequest> {

    private Class<?> clazz;
    private Serializer serializer;

    public RpcEncoder2(Class<?> clazz, Serializer serializer) {
        this.clazz = clazz;
        this.serializer = serializer;
    }

    protected void encode(ChannelHandlerContext ctx, RpcRequest msg, List<Object> out) throws Exception {
        System.out.println("编码2"+msg);
        if(clazz!=null && clazz.isInstance(msg)) {
            byte[] bytes = serializer.serialize(msg);
            System.out.println("编码2"+new String(bytes));
            out.add(serializer.serialize(bytes));
        }
    }
}
